package com.cndongtai.controller.report;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cndongtai.constant.LightConstant;
import com.cndongtai.constant.UserConstant;
import com.cndongtai.controller.BaseController;
import com.cndongtai.net.NetController;
import com.cndongtai.net.SocketContainer;
import com.cndongtai.net.redis.RedisServer;
import com.cndongtai.po.Dtu;
import com.cndongtai.po.Light;
import com.cndongtai.po.LightConfig;
import com.cndongtai.po.User;
import com.cndongtai.service.config.LightConfigService;
import com.cndongtai.service.light.DtuService;
import com.cndongtai.service.light.LightService;
import com.cndongtai.service.report.ReportService;
import com.cndongtai.util.ValidateUtil;
import com.cndongtai.vo.DtuBean;
import com.cndongtai.vo.LightBean;
import com.cndongtai.vo.LightChargeCountBean;
import com.cndongtai.vo.LightCurrentBean;
import com.cndongtai.vo.LightStatusBean;
import com.cndongtai.vo.PageForm;
import com.cndongtai.vo.UserChargeCountBean;

@Controller
public class ReportController extends BaseController {

	@Resource(name="reportService")
	private ReportService reportService;
	
	@Resource(name="dtuService")
	private DtuService dtuService;
	
	@Resource(name="lightService")
	private LightService lightService;
	
	@Resource(name="lightConfigService")
	private LightConfigService lightConfigService;
	
	@RequestMapping("/faultList")
	public String faultList() {
		// 获取故障报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		List<LightStatusBean> statusBeanList = reportService.queryFaultList(loginUser.getUserId());
		request.setAttribute("statusBeanList", statusBeanList);
		
		return "/report/faultList";
	}
	
	/**
	 * 支持分页查询
	 * @return
	 */
	@RequestMapping("/statusList")
	public String statusList() {
		PageForm<LightStatusBean> page = new PageForm<LightStatusBean>();
		// 获取状态报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		String nowPage = this.getValue("nowPage");
		if(ValidateUtil.empty(nowPage)) {
			page.setNowPage(1);
		}else {
			page.setNowPage(Integer.parseInt(nowPage));
		}
		
		String lightNum = this.getValue("lightNum");
		String panId = this.getValue("panId");
		page.addCondition("lightNum", lightNum);
		page.addCondition("panId", panId);
		
		if(!"admin".equals(loginUser.getUsername())) {
			page.addCondition("userId", loginUser.getUserId());
		}
		
		page = reportService.queryStatusListPage(page);
		
		request.setAttribute("page", page);
		request.setAttribute("lightNum", lightNum);
		request.setAttribute("panId", panId);
				
		return "/report/statusList";
	}
	
	/**
	 * 全屏自动刷新
	 * @return
	 */
	@RequestMapping("/statusListMonitor")
	public String statusListMonitor() {
		// 获取状态报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		String lightNum = this.getValue("mtLightNum");
		String panId = this.getValue("mtPanId");
		
		if("admin".equals(loginUser.getUsername())) {
			List<LightStatusBean> statusBeanList = reportService.queryStatusList(null, lightNum, panId);
			request.setAttribute("statusBeanList", statusBeanList);
		}else {
			List<LightStatusBean> statusBeanList = reportService.queryStatusList(loginUser.getUserId(), lightNum, panId);
			request.setAttribute("statusBeanList", statusBeanList);
		}
		
		request.setAttribute("mtLightNum", lightNum);
		request.setAttribute("mtPanId", panId);
				
		return "/report/statusListMonitor";
	}
	
	@RequestMapping("/queryLightLatestStatus")
	@ResponseBody
	public void queryLightLatestStatus() {
		// 获取状态报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		String lightId = this.getValue("lightId");
		
		LightStatusBean statusBean = null;
		
		if("admin".equals(loginUser.getUsername())) {
			statusBean = reportService.queryLightLatestStatus(null, lightId);
		}else {
			statusBean = reportService.queryLightLatestStatus(loginUser.getUserId(), lightId);
		}
		
		JSONObject result = new JSONObject();
		if(statusBean!=null) {
			result.put("success", 0);
			result.put("data", JSONObject.fromObject(statusBean));
		}else {
			result.put("success", 1);
			result.put("msg","未获取到该设备的状态");
		}
		
		this.writeData(result);
	}
	
	@RequestMapping("/queryLightLatestStatusNew")
	@ResponseBody
	public void queryLightLatestStatusNew() {
		
		// 获取状态报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		String lightId = this.getValue("lightId");
		
		LightStatusBean statusBean = null;
		
		if("admin".equals(loginUser.getUsername())) {
			statusBean = reportService.queryLightLatestStatus(null, lightId);
		}else {
			statusBean = reportService.queryLightLatestStatus(loginUser.getUserId(), lightId);
		}
		
		LightConfig lightConfig = lightConfigService.queryLightConfig(Integer.parseInt(lightId));
		
		JSONObject result = new JSONObject();
		if(statusBean!=null && lightConfig!=null) {
			JSONObject data = new JSONObject();
			data.put("id", statusBean.getLightNum());
			data.put("type", 1);
			
			Double solarPanelVol = Double.parseDouble(statusBean.getSolarPanelVol())*100;
			
			Double v1 = solarPanelVol/25;
			if(v1>100) {
				data.put("v1", 100);
			}else {
				data.put("v1", v1.intValue());
			}
			
			int maxWorkCurrent = 900;// 默认最大电流900ma
			if(lightConfig.getMaxWorkCurrent()!=null && lightConfig.getMaxWorkCurrent()==0) {
				maxWorkCurrent = lightConfig.getMaxWorkCurrent();
			}
			
			data.put("v2", Integer.parseInt(statusBean.getBatteryVolLevel()+"0"));//电池电量
			data.put("v3", (statusBean.getChargeCurrent()*100)/maxWorkCurrent);//充电电流
			data.put("v4", (statusBean.getWorkCurrent()*100)/maxWorkCurrent);//放电电流
			
			result.put("success", 0);
			result.put("data", data);
		}else {
			result.put("success", 1);
			result.put("msg","未获取到该设备的状态");
		}
		
		this.writeData(result);
	}
	
	@RequestMapping("/currentList")
	public String currentList() {
		// 获取故障报告列表
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		List<LightCurrentBean> currentBeanList = reportService.queryCurrentList(loginUser.getUserId());
		request.setAttribute("currentBeanList", currentBeanList);
				
		return "/report/currentList";
	}
	
	@RequestMapping("/data")
	public String data() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		
		// 获取路灯列表
		List<Dtu> dtuList = null;
		if("admin".equals(loginUser.getUsername())) {
			dtuList = dtuService.queryDtuList();
		}else {
			dtuList = dtuService.queryDtuListByUserId(loginUser.getUserId());
		}
		
		
		if(dtuList==null || dtuList.size()==0) {
			return "/report/data";
		}
		
		List<DtuBean> dtuBeanList = new ArrayList<DtuBean>();
		for(Dtu dtu : dtuList) {
			DtuBean dtuBean = new DtuBean();
			dtuBean.setDtuId(dtu.getDtuId());
			dtuBean.setDtuName(dtu.getPanId());
			
			List<LightBean> lightBeanList = lightService.queryLightBeanListByDtuId(dtu.getDtuId());
			dtuBean.setLightList(lightBeanList);
			
			dtuBeanList.add(dtuBean);
		}
		
		request.setAttribute("dtuBeanList", dtuBeanList);
		
		return "/report/data";
	}
	
	@RequestMapping("/report")
	public String report() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		
		// 获取路灯列表
		List<Dtu> dtuList = null;
		if("admin".equals(loginUser.getUsername())) {
			dtuList = dtuService.queryDtuList();
		}else {
			dtuList = dtuService.queryDtuListByUserId(loginUser.getUserId());
		}
		
		
		if(dtuList==null || dtuList.size()==0) {
			return "/report/report";
		}
		
		List<DtuBean> dtuBeanList = new ArrayList<DtuBean>();
		for(Dtu dtu : dtuList) {
			DtuBean dtuBean = new DtuBean();
			dtuBean.setDtuId(dtu.getDtuId());
			dtuBean.setDtuName(dtu.getPanId());
			
			List<LightBean> lightBeanList = lightService.queryLightBeanListByDtuId(dtu.getDtuId());
			dtuBean.setLightList(lightBeanList);
			
			dtuBeanList.add(dtuBean);
		}
		
		request.setAttribute("dtuBeanList", dtuBeanList);
		
		return "/report/report";
	}
	
	@RequestMapping("/leastcharge")
	public String leastcharge() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		PageForm<LightChargeCountBean> page = new PageForm<LightChargeCountBean>();
		
		String nowPage = this.getValue("nowPage");
		if(ValidateUtil.empty(nowPage)) {
			page.setNowPage(1);
		}else {
			page.setNowPage(Integer.parseInt(nowPage));
		}
		
		String lightNum = this.getValue("lightNum");
		String panId = this.getValue("panId");
		page.addCondition("lightNum", lightNum);
		page.addCondition("panId", panId);
		
		if(!"admin".equals(loginUser.getUsername())) {
			page.addCondition("userId", loginUser.getUserId());
		}
		
		page = reportService.queryLightChargeCountListPage(page);

		request.setAttribute("page", page);
		
		return "/report/report_leastcharge";
	}
	
	@RequestMapping("/usercharge")
	public String usercharge() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		PageForm<UserChargeCountBean> page = new PageForm<UserChargeCountBean>();
		
		String nowPage = this.getValue("nowPage");
		if(ValidateUtil.empty(nowPage)) {
			page.setNowPage(1);
		}else {
			page.setNowPage(Integer.parseInt(nowPage));
		}
		
		String companyName = this.getValue("companyName");
		String mobile = this.getValue("mobile");
		
		page.addCondition("companyName", companyName);
		page.addCondition("mobile", mobile);
		
		if(!"admin".equals(loginUser.getUsername())) {
			page.addCondition("userId", loginUser.getUserId());
		}
		
		page = reportService.queryUserChargeCountListPage(page);

		request.setAttribute("companyName", companyName);
		request.setAttribute("mobile", mobile);
		request.setAttribute("page", page);
		
		return "/report/report_usercharge";
	}
	
	@RequestMapping("/datashow")
	public String datashow() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		
		String startTime = this.getValue("startTime");
		String endTime = this.getValue("endTime");
		String time = this.getValue("time");
		String lightId = this.getValue("lightId");
		String dataType = this.getValue("dataType");
		
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		if("0".equals(time)) {
			startTime = format.format(c.getTime()) + " 00:00";
			endTime = format.format(c.getTime()) + " 23:59";
		}else if("1".equals(time)) {
			endTime = format.format(c.getTime()) + " 23:59";
			c.add(Calendar.DATE, -7);
			startTime = format.format(c.getTime()) + " 00:00";
		}else if("2".equals(time)) {
			endTime = format.format(c.getTime()) + " 23:59";
			c.add(Calendar.DATE, -30);
			startTime = format.format(c.getTime()) + " 00:00";
		}else {
			startTime = format.format(c.getTime()) + " 00:00";
			endTime = format.format(c.getTime()) + " 23:59";
		}
		
		// 获取路灯列表
		if("batteryVol".equals(dataType)) {
			String[] dataArr = reportService.queryBatteryVolData(startTime, endTime, Long.valueOf(lightId));
			// 蓄电池电压
			
			Light light = lightService.queryLightById(lightId);
			request.setAttribute("deviceName", "Battery Vol-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit", "电池电压");
			request.setAttribute("enUnit", " V");
			request.setAttribute("ymin", "0");
			request.setAttribute("ymax", "20");
			request.setAttribute("yData", dataArr[0]);
			request.setAttribute("xData", dataArr[1]);
			request.setAttribute("title", "Battery Vol");
		}else if("temperature".equals(dataType)) {
			String[] dataArr = reportService.queryTemperatureData(startTime, endTime, Long.valueOf(lightId));
			// 蓄电池温度
			
			Light light = lightService.queryLightById(lightId);
			request.setAttribute("deviceName", "Temperature-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit", "温度");
			request.setAttribute("enUnit", " °C");
			request.setAttribute("ymin", "20");
			request.setAttribute("ymax", "32");
			request.setAttribute("yData", dataArr[0]);
			request.setAttribute("xData", dataArr[1]);
			request.setAttribute("title", "Battery Temperature");
			
		}else if("solarPanelVol".equals(dataType)) {
			String[] dataArr = reportService.querySolarPanelVolData(startTime, endTime, Long.valueOf(lightId));
			// 太阳板电压
			
			Light light = lightService.queryLightById(lightId);
			request.setAttribute("deviceName", "SolarPanel Vol-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit", "太阳板电压");
			request.setAttribute("enUnit", " V");
			request.setAttribute("ymin", "0");
			request.setAttribute("ymax", "25");
			request.setAttribute("yData", dataArr[0]);
			request.setAttribute("xData", dataArr[1]);
			request.setAttribute("title", "SolarPanel Vol");
		}else if("chargeCurrent".equals(dataType)) {
			String[] dataArr = reportService.queryChargeCurrentData(startTime, endTime, Long.valueOf(lightId));
			// 充电电流
			
			Light light = lightService.queryLightById(lightId);
			request.setAttribute("deviceName", "Charge Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit", "充电电流");
			request.setAttribute("enUnit", " mA");
			request.setAttribute("ymin", "0");
			request.setAttribute("ymax", "5000");
			request.setAttribute("yData", dataArr[0]);
			request.setAttribute("xData", dataArr[1]);
			request.setAttribute("title", "Charge Current");
		}else if("workCurrent".equals(dataType)) {
			String[] dataArr = reportService.queryWorkCurrentData(startTime, endTime, Long.valueOf(lightId));
			// 放电电流
			
			Light light = lightService.queryLightById(lightId);
			request.setAttribute("deviceName", "Work Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit", "放电电流");
			request.setAttribute("enUnit", " mA");
			request.setAttribute("ymin", "0");
			request.setAttribute("ymax", "1000");
			request.setAttribute("yData", dataArr[0]);
			request.setAttribute("xData", dataArr[1]);
			request.setAttribute("title", "Work Current");
		}else {
			request.setAttribute("deviceName", "Type-(灯杆编号)0001");
			request.setAttribute("cnUnit", "Type");
			request.setAttribute("enUnit", " unit");
			request.setAttribute("ymin", "0");
			request.setAttribute("ymax", "1000");
			request.setAttribute("yData", "[]");
			request.setAttribute("xData", "[]");
			request.setAttribute("title", "Type");
		}
		
		return "/report/data_right";
	}
	
	@RequestMapping("/dataNew")
	public String dataNew() {
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		
		String startTime = this.getValue("startTime");
		String endTime = this.getValue("endTime");
		String time = this.getValue("dateSelect");
		String lightId = this.getValue("lightId");
		
		if(time==null || "".equals(time)) {
			time = "0";
		}
		
		// 获取路灯列表
		List<Dtu> dtuList = null;
		if("admin".equals(loginUser.getUsername())) {
			dtuList = dtuService.queryDtuList();
		}else {
			dtuList = dtuService.queryDtuListByUserId(loginUser.getUserId());
		}
		
		if(dtuList==null || dtuList.size()==0) {
			return "/report/dataNew";
		}
		
		List<DtuBean> dtuBeanList = new ArrayList<DtuBean>();
		for(Dtu dtu : dtuList) {
			DtuBean dtuBean = new DtuBean();
			dtuBean.setDtuId(dtu.getDtuId());
			dtuBean.setDtuName(dtu.getPanId());
			
			if(dtu.getDtuType()==0) {
				SocketContainer container = NetController.getSocketPool().get(dtu.getPanId());
				if(container==null) {
					dtuBean.setOnlineState(1);
				}else {
					dtuBean.setOnlineState(0);
				}
			}
			
			List<LightBean> lightBeanList = lightService.queryLightBeanListByDtuId(dtu.getDtuId());
			dtuBean.setLightList(lightBeanList);
			
			if(lightId==null || "".equals(lightId)) {
				if(lightBeanList.size()>0) {
					lightId = lightBeanList.get(0).getLightId()+"";
				}
			}
			
			dtuBeanList.add(dtuBean);
		}
		
		request.setAttribute("dtuBeanList", dtuBeanList);
		request.setAttribute("time", time);
		request.setAttribute("lightId", lightId);
				
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		if("0".equals(time)) {
			startTime = format.format(c.getTime()) + " 00:00";
			endTime = format.format(c.getTime()) + " 23:59";
		}else if("1".equals(time)) {
			endTime = format.format(c.getTime()) + " 23:59";
			c.add(Calendar.DATE, -7);
			startTime = format.format(c.getTime()) + " 00:00";
		}else if("2".equals(time)) {
			endTime = format.format(c.getTime()) + " 23:59";
			c.add(Calendar.DATE, -30);
			startTime = format.format(c.getTime()) + " 00:00";
		}else {
			startTime = format.format(c.getTime()) + " 00:00";
			endTime = format.format(c.getTime()) + " 23:59";
		}
		
		// 获取路灯列表
		Light light = lightService.queryLightById(lightId);
		//if("batteryVol".equals(dataType)) {
			String[] dataArr_batteryVol = reportService.queryBatteryVolData(startTime, endTime, Long.valueOf(lightId));
			// 蓄电池电压
			
			request.setAttribute("deviceName_batteryVol", "Battery Vol-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_batteryVol", "电池电压");
			request.setAttribute("enUnit_batteryVol", " V");
			request.setAttribute("ymin_batteryVol", "0");
			request.setAttribute("ymax_batteryVol", "20");
			request.setAttribute("yData_batteryVol", dataArr_batteryVol[0]);
			request.setAttribute("xData_batteryVol", dataArr_batteryVol[1]);
			request.setAttribute("title_batteryVol", "Battery Vol");
		//}else if("temperature".equals(dataType)) {
			String[] dataArr_temperature = reportService.queryTemperatureData(startTime, endTime, Long.valueOf(lightId));
			// 蓄电池温度
			
			request.setAttribute("deviceName_temperature", "Temperature-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_temperature", "温度");
			request.setAttribute("enUnit_temperature", " °C");
			request.setAttribute("ymin_temperature", "20");
			request.setAttribute("ymax_temperature", "32");
			request.setAttribute("yData_temperature", dataArr_temperature[0]);
			request.setAttribute("xData_temperature", dataArr_temperature[1]);
			request.setAttribute("title_temperature", "Battery Temperature");
			
		//}else if("solarPanelVol".equals(dataType)) {
			String[] dataArr_solarPanelVol = reportService.querySolarPanelVolData(startTime, endTime, Long.valueOf(lightId));
			// 太阳板电压
			
			request.setAttribute("deviceName_solarPanelVol", "SolarPanel Vol-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_solarPanelVol", "太阳板电压");
			request.setAttribute("enUnit_solarPanelVol", " V");
			request.setAttribute("ymin_solarPanelVol", "0");
			request.setAttribute("ymax_solarPanelVol", "25");
			request.setAttribute("yData_solarPanelVol", dataArr_solarPanelVol[0]);
			request.setAttribute("xData_solarPanelVol", dataArr_solarPanelVol[1]);
			request.setAttribute("title_solarPanelVol", "SolarPanel Vol");
		//}else if("chargeCurrent".equals(dataType)) {
			String[] dataArr_chargeCurrent = reportService.queryChargeCurrentData(startTime, endTime, Long.valueOf(lightId));
			// 充电电流
			
			request.setAttribute("deviceName_chargeCurrent", "Charge Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_chargeCurrent", "充电电流");
			request.setAttribute("enUnit_chargeCurrent", " mA");
			request.setAttribute("ymin_chargeCurrent", "0");
			request.setAttribute("ymax_chargeCurrent", "5000");
			request.setAttribute("yData_chargeCurrent", dataArr_chargeCurrent[0]);
			request.setAttribute("xData_chargeCurrent", dataArr_chargeCurrent[1]);
			request.setAttribute("title_chargeCurrent", "Charge Current");
		//}else if("workCurrent".equals(dataType)) {
			String[] dataArr_workCurrent = reportService.queryWorkCurrentData(startTime, endTime, Long.valueOf(lightId));
			// 放电电流
			
			request.setAttribute("deviceName_workCurrent", "Work Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_workCurrent", "放电电流");
			request.setAttribute("enUnit_workCurrent", " mA");
			request.setAttribute("ymin_workCurrent", "0");
			request.setAttribute("ymax_workCurrent", "1000");
			request.setAttribute("yData_workCurrent", dataArr_workCurrent[0]);
			request.setAttribute("xData_workCurrent", dataArr_workCurrent[1]);
			request.setAttribute("title_workCurrent", "Work Current");
			
			// 充电量
			String[] dataArr_chargeCount = reportService.queryChargeCountData(startTime, endTime, Long.valueOf(lightId));
			request.setAttribute("deviceName_chargeCount", "Work Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_chargeCount", "放电电流");
			request.setAttribute("enUnit_chargeCount", " mA");
			request.setAttribute("ymin_chargeCount", "0");
			request.setAttribute("ymax_chargeCount", "1000");
			request.setAttribute("yData_chargeCount", dataArr_chargeCount[0]);
			request.setAttribute("xData_chargeCount", dataArr_chargeCount[1]);
			request.setAttribute("title_chargeCount", "Work Current");
			
			// 放电量
			String[] dataArr_workCount = reportService.queryWorkCountData(startTime, endTime, Long.valueOf(lightId));
			request.setAttribute("deviceName_workCount", "Work Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_workCount", "放电电流");
			request.setAttribute("enUnit_workCount", " mA");
			request.setAttribute("ymin_workCount", "0");
			request.setAttribute("ymax_workCount", "1000");
			request.setAttribute("yData_workCount", dataArr_workCount[0]);
			request.setAttribute("xData_workCount", dataArr_workCount[1]);
			request.setAttribute("title_workCount", "Work Current");
			
			// 电池电量
			String[] dataArr_batteryQuantity = reportService.queryBatteryQuantityData(startTime, endTime, Long.valueOf(lightId));
			request.setAttribute("deviceName_batteryQuantity", "Work Current-(灯杆编号)" + light.getLightNum());
			request.setAttribute("cnUnit_batteryQuantity", "放电电流");
			request.setAttribute("enUnit_batteryQuantity", " mA");
			request.setAttribute("ymin_batteryQuantity", "0");
			request.setAttribute("ymax_batteryQuantity", "1000");
			request.setAttribute("yData_batteryQuantity", dataArr_batteryQuantity[0]);
			request.setAttribute("xData_batteryQuantity", dataArr_batteryQuantity[1]);
			request.setAttribute("title_batteryQuantity", "Work Current");
						
			// 获取实时数据
			
		
		return "/report/dataNew";
	}
	
	@RequestMapping("/queryCountData")
	@ResponseBody
	public void queryCountData() {
		JSONObject result = new JSONObject();
		User loginUser = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		String dtuId = this.getValue("dtuId");
		RedisServer redis = RedisServer.getInstance();
		
		List<Dtu> dtuList = null;
		if("admin".equals(loginUser.getUsername())) {
			dtuList = dtuService.queryDtuList();
		}else {
			dtuList = dtuService.queryDtuListByUserId(loginUser.getUserId());
		}
		
		if(dtuList==null || dtuList.size()==0) {
			result.put("dtucount", 0);
			result.put("lightcount", 0);
			result.put("onlinecount", 0);
			result.put("faultcount", 0);
			this.writeData(result);
			return;
		}
		
		int dtucount = 0;
		int lightcount = 0;
		int onlinecount = 0;
		
		for(Dtu dtu : dtuList) {
			if("0".equals(dtuId) || dtu.getDtuId().equals(Long.valueOf(dtuId))) {
				dtucount ++;
				List<LightBean> lightList = lightService.queryLightBeanListByDtuId(dtu.getDtuId());
				for(LightBean light : lightList) {
					lightcount ++;
					/*if(light.getOnlineState().equals(0)) {
						onlinecount ++;
					}*/
					if(redis.get(LightConstant.REDIS_LIGHT_KEY + dtu.getPanId() + "_" + light.getLightNum())!=null) {
						onlinecount ++;
					}
					
				}
			}
		}
		
		// 获取当前状态
		
		
		result.put("success", 0);
		result.put("dtucount", dtucount);
		result.put("lightcount", lightcount);
		result.put("onlinecount", onlinecount);
		result.put("faultcount", lightcount-onlinecount);
		
		this.writeData(result);
	}
}
